import { loginApi } from "@/api/user";

const state = {
  token: null,
  userInfo: {},
};

const mutations = {
  setToken(state, token) {
    // console.log(token);
    state.token = token;
  },
  removeToken(state) {
    state.token = null;
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo;
  },
  removeUserInfo(state) {
    state.userInfo = {};
  },
};

const actions = {
  async loginAction({ commit }, data) {
    const res = await loginApi(data);
    // console.log(res);
    commit("setToken", res.token);
    commit("setUserInfo", res);
  },
  delAction({ commit }) {
    commit("removeToken");
    commit("removeUserInfo");
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
